//
//  HWGiftBagCenterHttpManager.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/22.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import Alamofire
class HWGiftBagCenterHttpManager: NSObject {
    var uid = HWLocallyStoredData.getLoginUserID()
    var tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
    var p = 1
    var max_p = "1"
}
extension HWGiftBagCenterHttpManager {
    /// sign加密
    /// - Returns: sign
    func encrySign() -> String {
        
        let sign = "\(uid)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
        let signMD5 = WMUtil.md5HexDigest(sign) ?? ""
        return signMD5
    }
    func headers() -> HTTPHeaders {
        //        let tStr = Date().timeStamp
        //        let signature = "nnmitu"+tStr
        //
        //        let json = ["usersid":HWLocallyData.getUsersID(),"timestamp":tStr,"signature":signature.DDMD5Encrypt(.lowercase32)]
        //        let jsonString = HWPublicFuncTools.convertDictionAryToJson(dic: json as NSDictionary)
        //        let aesStr:String = aesEncryptString(jsonString, "NrrynIXcpucYvGSk")
        let header:HTTPHeaders = ["eaua":JJUUIDManager.getUUID(),"imei":ASIdentifierManager.shared().advertisingIdentifier.uuidString]
        
        return header
    }
}
extension HWGiftBagCenterHttpManager {
    
    /// 礼包列表
    func getGiftBagCenterList(type:String,keyword:String,success:@escaping (_ models:[HWGiftBagCenterModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "ios":"1",
            "type":type,
            "keyword":keyword,
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GiftBagCenterList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGiftBagCenterModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGiftBagCenterModel])
                            
                        }else {
                            failure(resultDic["message"] as? String ?? "")
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 礼包列表
    func getGiftBagCenterListFooter(type:String,keyword:String,success:@escaping (_ models:[HWGiftBagCenterModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p += 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "p":p,
            "sign":encrySign(),
            "ios":"1",
            "type":type,
            "keyword":keyword,
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GiftBagCenterList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGiftBagCenterModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGiftBagCenterModel])
                            
                        }else {
                            failure(resultDic["message"] as? String ?? "")
                            self.p -= 1
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                        self.p -= 1
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                        self.p -= 1
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    self.p -= 1
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                                self.p -= 1
            }
        }
    }

    /// 礼包详情
    func getGiftBagCenterDetail(gid:String,success:@escaping (_ model:HWGiftBagCenterDetailModel) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "ios":"1",
            "gid":gid,
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GiftBagCenterDetail)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = HWGiftBagCenterDetailModel.deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model)
                            
                        }else {
                            failure(resultDic["message"] as? String ?? "")
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 礼包领取
    func getGiftBagReceive(id:String,success:@escaping (_ success:Bool) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "ios":"1",
            "id":id,
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GiftBagReceive)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        success(true)
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
}
